﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class BaseUi : MonoBehaviour
{
	[HideInInspector]
	public bool uiBtnIsPrass; //是否有按钮正被按下，防止连按
	protected bool isPlayAnction; //是否有按钮正被按下，防止连按
	public virtual void onClickNoAnction(GameObject obj)
	{
		if (uiBtnIsPrass) {
			return;
		}
		if (isPlayAnction) {
			return;
		}
		_onClick(obj.name, obj);
	}
	public virtual void onClickNone(GameObject obj)
	{
		if (uiBtnIsPrass) {
			return;
		}
		if (isPlayAnction) {
			return;
		}
		uiBtnIsPrass = true;
		float oneTime = 0.02f;
		var rect = obj.GetComponent<RectTransform>();
		Vector3 scale = rect.localScale;
		Sequence mySequence = DOTween.Sequence(); //创建空序列 
		Tweener anction0 = rect.DOScale(scale * 0.94f, oneTime * 5).SetEase(Ease.Linear);
		Tweener anction1 = rect.DOScale(scale, oneTime * 5).SetEase(Ease.Linear);
		mySequence.Append(anction0);
		mySequence.Append(anction1);
		mySequence.AppendCallback(() => {
			_onClick(obj.name, obj);
			uiBtnIsPrass = false;
		});
	}
	//点击事件
	public virtual void _onClick(string btnTag, GameObject obj)
	{

	}
	//显示ui
	public void showUi(GameObject obj, System.Action cb = null)
	{
		Image zeZhao = obj.transform.GetChild(0).GetComponent<Image>();
		zeZhao.color = new Color(0, 0, 0, 0);
		zeZhao.DOColor(new Color(0, 0, 0, 0.5f), 0.4f);
		obj.SetActive(true);
		isPlayAnction = true;
		RectTransform rect = obj.transform.GetChild(1).GetComponent<RectTransform>();
		rect.localScale = Vector3.zero;
		rect.DOScale(Vector3.one, 0.4f).SetEase(Ease.OutBack).OnComplete(() => {
			isPlayAnction = false;
			if (cb != null) {
				cb();
			}
		});
	}
	//关闭开始界面
	public void closeUi(GameObject obj, System.Action cb = null)
	{
		Image zeZhao = obj.transform.GetChild(0).GetComponent<Image>();
		zeZhao.DOColor(new Color(0, 0, 0, 0), 0.6f);
		isPlayAnction = true;
		RectTransform rect = obj.transform.GetChild(1).GetComponent<RectTransform>();
		rect.DOScale(Vector3.zero, 0.4f).SetEase(Ease.InBack).OnComplete(() => {
			isPlayAnction = false;
			obj.SetActive(false);
			if (cb != null) {
				cb();
			}
		});
	}

}